package com.owen.websocket.basic.controller;

import com.owen.websocket.basic.websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/websocket")
public class WebSocketController {

    @Autowired
    private WebSocket webSocket;

    @GetMapping("/sendMessage")
    public String sendMessage(@RequestParam(value = "userId",required = false) String userId,String data){
        if(StringUtils.isEmpty(userId)){
            webSocket.sendAllMessage(data);
        }else{
            webSocket.sendOneMessage(userId,data);
        }
        return data;
    }

}
